package com.app.studyroom.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import com.app.studyroom.R;
import com.app.studyroom.api.model.ssrUser.UserItem;
import com.app.studyroom.dialog.UserEditDialog;
import com.app.studyroom.mapper.AreaNameToIdMapper;

import java.util.List;

public class UserAdapter extends RecyclerView.Adapter<UserAdapter.UserViewHolder> {

    private List<UserItem> userList;
    private Context context;
    private OnUserActionListener listener;

    public UserAdapter(Context context, List<UserItem> userList, OnUserActionListener listener) {
        this.context = context;
        this.userList = userList;
        this.listener = listener;
    }

    public void setUserList(List<UserItem> userList) {
        this.userList = userList;
        notifyDataSetChanged();
    }

    @Override
    public UserViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_user_manage, parent, false);
        return new UserViewHolder(view);
    }

    @Override
    public void onBindViewHolder(UserViewHolder holder, int position) {

        UserItem user = userList.get(position);
//        String areaId =user.getQy();

        holder.tvUserName.setText(user.getUserName());
        holder.tvUserZjh.setText(user.getUserZjh());
        holder.tvUserNumber.setText(user.getUserNumber());
        holder.tvUserSjh.setText(user.getUserSjh());
        holder.tvUserQy.setText(user.getQy());
        holder.tvUserBs.setText(user.getUserBs());

        holder.btnEdit.setOnClickListener(v -> {
            UserEditDialog.showEditDialog(context, user, updatedUser -> {
                if (listener != null) {
                    listener.onEdit(updatedUser);
                }
            });
        });

        holder.btnDelete.setOnClickListener(v -> {
            if (listener != null) {
                listener.onDelete(user.getId());
            }
        });
    }

    @Override
    public int getItemCount() {
        return userList == null ? 0 : userList.size();
    }

    static class UserViewHolder extends RecyclerView.ViewHolder {
        TextView tvUserName, tvUserZjh, tvUserNumber, tvUserSjh, tvUserQy, tvUserBs;
        Button btnEdit, btnDelete;

        public UserViewHolder(View itemView) {
            super(itemView);
            tvUserName = itemView.findViewById(R.id.tvUserName);
            tvUserZjh = itemView.findViewById(R.id.tvUserZjh);
            tvUserNumber = itemView.findViewById(R.id.tvUserNumber);
            tvUserSjh = itemView.findViewById(R.id.tvUserSjh);
            tvUserQy = itemView.findViewById(R.id.tvUserQy);
            tvUserBs = itemView.findViewById(R.id.tvUserBs);
            btnEdit = itemView.findViewById(R.id.btnEdit);
            btnDelete = itemView.findViewById(R.id.btnDelete);
        }
    }

    public interface OnUserActionListener {
        void onEdit(UserItem user);
        void onDelete(long userId);
    }
}
